﻿<?php
	require_once("mysql_help.class.php");
	class Inbox
	{
		var $sqlHelper;
		
		//构造函数
		public function __construct()
		{
			$this->sqlHelper = new db_Mysql();
		}
		
		/*功能：根据选择的条件找到对应的毕业生，将记录批量插入数据库
		//$majorId(int)  专业id(0代表无专业要求)
		//$school(int)	学校id(0代表无专业要求)
		//$sex(int)  性别(0代表无性别要求，0为男，1为女)
		//$enrol_year(int)  入学年份(0代表无入学年份要求)
		//$id_employ_message(int)  招聘信息的id(这个必须有且>0)
		//返回：返回true代表执行成功
		*/
		public function SendMessageTo($majorId,$school,$sex,$enrol_year,$id_employ_message)
		{
			$major_choose = $school_choose = $sex_choose = $enrol_year_choose = $default_choose = null;
			
			//专业
			if($majorId == 0) $major_choose = "";//对专业无要求
			else $major_choose = " or (major = '".$majorId."') ";
			
			//学校
			if($school == 0) $school_choose = "";//无学校要求
			else $school_choose = " or (school = '".$school."') ";
			
			//性别
			if($sex == 0) $sex_choose = "";//对性别无要求
			else $sex_choose = " or (sex = '".$sex."') ";
			
			//入学年份(前台可以写成毕业年份)
			if($enrol_year == 0) $enrol_year_choose = "";//对入学年份无要求
			else $enrol_year_choose = " or (enrol_year = '".$enrol_year."') ";
			
			if($majorId != 0 || $school != 0 || $sex != 0 || $enrol_year != 0) $default_choose = "";
			else $default_choose = " or (1 = 1) ";//如果都无要求  那么or true
			
			//查找出所有符合条件的毕业生的id,然后依次取出执行insert函数
			$this->sqlHelper->dbconnect();
			$cmd_select = "select id from graduate where 1 = 0".$major_choose.$school_choose.$sex_choose.$enrol_year_choose.$default_choose;
			//echo '<script>alert("'.$cmd_select.'");</script>';
			$time = time();
			$cmd_insert_undone = "insert into inbox(is_read,date_public,id_employ_message,id_graduate) values( 0,'".$time."','".$id_employ_message."','";//未添加毕业生id
			$result = $this->sqlHelper->execute($cmd_select);
			while($row = $this->sqlHelper->fetch_array($result))
			{
				$cmd_insert = $cmd_insert_undone.$row["id"]."')";//添加毕业生的id
				//echo '<script>alert("'.$cmd_insert.'");</script>';
				$this->sqlHelper->insert($cmd_insert);
			}
			$this->sqlHelper->free_result($result);
			$this->sqlHelper->dbclose();
			return true;
		}
		
		/*根据inbox的id，来查找该信息
		//$messageId(int)  inbox信息的id
		//返回result，使用完记得free哦！
		*/
		public function ReadMessageById($messageId)
		{
			$cmd = "select * from inbox where id = '".$messageId."'";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			return $result;
		}
	
		
		/*功能：根据毕业生的id，搜索属于该用户的所有信息
		@$userId(int)  毕业生的id
		//注意：返回的$result使用完要记得free哦
		*/
		public function GetAllMessageByUserId($userId)
		{
			$cmd = 
				"select inbox_employMessage_company.*,city.name as name_city
				from
				(
					select inbox_employMessage.*,company.companyName as name_company
					from (
						select inbox.*,employ_message.company_id,employ_message.position,employ_message.place,employ_message.payment,employ_message.job_attribute
						from inbox left join employ_message on inbox.id_employ_message = employ_message.id where inbox.id_graduate = '".$userId."'
					)as inbox_employMessage
					left join company on inbox_employMessage.company_id = company.id
				)as inbox_employMessage_company
				left join city on inbox_employMessage_company.place = city.id
				order by date_public desc";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			return $result;
		}
		
		public function ShowMessageById($userId)
		{
			$result = $this->GetAllMessageByUserId($userId);
			$str = "";
			$num = (int)$this->sqlHelper->num_rows($result);//收信箱记录数
			//echo '<script>alert("'.$num.'");</script>';
			if($num > 0)
			{
				while($row = $this->sqlHelper->fetch_array($result))
				{
					$job_name = "";
					$job_num = (int)$row["job_attribute"];
					//echo '<script>alert("'.$row["job_attribute"].'");</script>';
					if($job_num == 1)
					{
						$job_name = "全职";
					}
					else if($job_num == 2)
					{
						$job_name = "兼职";
					}
					$str .=
					'
					<div class="inbox_Recruitment_list_contain">
						<div class="inbox_Recruitment_list_name">
							<div class="left fc_cpn_names">'.$row["name_company"].'</div>
							<div class="left">&nbsp&nbsp向您发来招聘信息</div>
							<div class="inbox_company_recruitment_check"><a href="Recruitment_detail.php?id_employMsg='.$row["id_employ_message"].'&graduateId='.$userId.'">
							查看详情</a> | 删除</div>
						</div>
						<div class="inbox_Recruitment_list_position">职位名称：<span class="fc333">'.$row["position"].' ('.$job_name.')</span></div>
						<div class="inbox_Recruitment_list_location">工作地点：<span class="fc333">'.$row["name_city"].'</span></div>
						<div class="inbox_Recruitment_list_endtime">薪资待遇：<span class="fc333">RMB '.$row["payment"].'/月</span></div>
						<div class="clear"></div>
					</div>
					';
				}
				$this->sqlHelper->free_result($result);
				return $str;
			}
			else
			{
				$this->sqlHelper->free_result($result);
				return '木有记录';
			}
		}
		
		/*根据inbox的id，来删除该信息
		//$messageId(int)  inbox信息的id
		//成功返回true
		*/
		public function DeleteMessageById($messageId)
		{
			$cmd = "delete * from inbox where id = '".$messageId."'";
			$this->sqlHelper->dbconnect();
			$this->sqlHelper->delete($cmd);
			$this->sqlHelper->dbclose();
			return true;
		}
	}
?>
			
			
		